﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;

namespace ArtLand.OA.Project.HR
{
    public partial class HR_InsuranceChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBaseHelper.BindYear(Drp_Year);
                DataBaseHelper.BindMonth(Drp_Mouth);
                Drp_Year.SelectedValue = DateTime.Now.Year.ToString();
                Drp_Mouth.SelectedValue = DateTime.Now.AddMonths(-1).Month.ToString();// (DateTime.Now.a.Month - 1).ToString();
                LoadData();
            }
            
            //this.Load += new System.EventHandler(this.Calculation);
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["empID"]))
            {
                HR_EMP he = TbsDbSession.Default.From<HR_EMP>().Where(HR_EMP._.EMPLID == Request["empID"].Trim()).ToFirst();
                SimpleForm1.Title = he.EMNAME + " - " + SimpleForm1.Title;
                HR_Insurance _inSurance = TbsDbSession.Default.From<HR_Insurance>().Where(HR_Insurance._.EmpNo == Request["empID"].Trim()).ToFirst();
                if (_inSurance != null)
                {
                    txt_PayType.Text = _inSurance.PayType;
                    txt_PayStuts.Text = _inSurance.PayStuts;
                    txt_PayStartMonth.Text = _inSurance.PayStartMonth;
                    txt_SocialBase.Text = _inSurance.SocialBase.ToString();
                    txt_ProvBase.Text = _inSurance.ProvBase.ToString();
                }
            }
            if (!string.IsNullOrEmpty(Request["ID"]))
            {
                HR_Insurance _inSurance = TbsDbSession.Default.From<HR_Insurance>().Where(HR_Insurance._.ID == int.Parse(Request["id"])).ToFirst();
                Drp_Mouth.SelectedValue = _inSurance.PayMonth.Value.ToString();
                Drp_Year.SelectedValue = _inSurance.PayYear.Value.ToString();
                txt_PayType.Text = _inSurance.PayType;
                txt_PayStuts.Text = _inSurance.PayStuts;
                txt_PayStartMonth.Text = _inSurance.PayStartMonth;
                txt_SocialBase.Text = _inSurance.SocialBase.ToString();
                txt_ProvBase.Text = _inSurance.ProvBase.ToString();
                txt_NursPerson.Text = _inSurance.NursPerson.ToString();
                txt_MedicalPerson.Text = _inSurance.MedicalPerson.ToString();
                txt_UnemployePerson.Text = _inSurance.UnemployePerson.ToString();
                txt_NursCompany.Text = _inSurance.NursCompany.ToString();
                txt_MedicalCompany.Text = _inSurance.MedicalCompany.ToString();
                txt_UnemployeCompany.Text = _inSurance.UnemployeCompany.ToString();
                txt_BirthCompany.Text = _inSurance.BirthCompany.ToString();
                txt_InjuryCompany.Text = _inSurance.InjuryCompany.ToString();
                txt_Memo.Text = _inSurance.Memo;
                CalculationVoid();
            }
            
        }


        protected void Drp_Year_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            HR_Insurance _inSurance = new HR_Insurance();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                _inSurance.ID = int.Parse(Request["id"]);
                _inSurance.Attach();
                _inSurance.PayMonth = int.Parse(Drp_Mouth.SelectedValue);
                _inSurance.PayYear = int.Parse(Drp_Year.SelectedValue);
                _inSurance.PayType = txt_PayType.Text;
                _inSurance.PayStuts = txt_PayStuts.Text;
                _inSurance.PayStartMonth = txt_PayStartMonth.Text;
                _inSurance.SocialBase = decimal.Parse(txt_SocialBase.Text);
                _inSurance.ProvBase = decimal.Parse(txt_ProvBase.Text);
                _inSurance.NursPerson = decimal.Parse(txt_NursPerson.Text);
                _inSurance.MedicalPerson = decimal.Parse(txt_MedicalPerson.Text);
                _inSurance.UnemployePerson = decimal.Parse(txt_UnemployePerson.Text);
                _inSurance.PersonTotal = decimal.Parse(RepalceStringFormat(lbl_PersonTotal));
                _inSurance.NursCompany = decimal.Parse(txt_NursCompany.Text);
                _inSurance.MedicalCompany = decimal.Parse(txt_MedicalCompany.Text);
                _inSurance.UnemployeCompany = decimal.Parse(txt_UnemployeCompany.Text);
                _inSurance.BirthCompany = decimal.Parse(txt_BirthCompany.Text);
                _inSurance.InjuryCompany = decimal.Parse(txt_InjuryCompany.Text);
                _inSurance.CompanyTotal = decimal.Parse(RepalceStringFormat(lbl_CompanyTotal));
                _inSurance.SocialTotal = decimal.Parse(RepalceStringFormat(lbl_SocialTotal));
                _inSurance.ProvPerson = decimal.Parse(RepalceStringFormat(lbl_ProvPerson));
                _inSurance.ProvCompany = decimal.Parse(RepalceStringFormat(lbl_ProvCompany));
                _inSurance.ProvTotal = decimal.Parse(RepalceStringFormat(lbl_ProvTotal));
                _inSurance.CompanyAmount = decimal.Parse(RepalceStringFormat(lbl_CompanyAmount));
                _inSurance.PersonAmount = decimal.Parse(RepalceStringFormat(lbl_PersonAmount));
                _inSurance.Amounts = decimal.Parse(RepalceStringFormat(lbl_Amounts));
                _inSurance.Memo = txt_Memo.Text;
                returnvalue = TbsDbSession.Default.Update<HR_Insurance>(_inSurance);
            }
            else
            {
                _inSurance.DeAttach();
                _inSurance.EmpNo = Request["empID"];
                _inSurance.PayMonth = int.Parse(Drp_Mouth.SelectedValue);
                _inSurance.PayYear = int.Parse(Drp_Year.SelectedValue);
                _inSurance.PayType = txt_PayType.Text;
                _inSurance.PayStuts = txt_PayStuts.Text;
                _inSurance.PayStartMonth = txt_PayStartMonth.Text;
                _inSurance.SocialBase = decimal.Parse(txt_SocialBase.Text);
                _inSurance.ProvBase = decimal.Parse(txt_ProvBase.Text);
                _inSurance.NursPerson = decimal.Parse(txt_NursPerson.Text);
                _inSurance.MedicalPerson = decimal.Parse(txt_MedicalPerson.Text);
                _inSurance.UnemployePerson = decimal.Parse(txt_UnemployePerson.Text);
                _inSurance.PersonTotal = decimal.Parse(RepalceStringFormat(lbl_PersonTotal));
                _inSurance.NursCompany = decimal.Parse(txt_NursCompany.Text);
                _inSurance.MedicalCompany = decimal.Parse(txt_MedicalCompany.Text);
                _inSurance.UnemployeCompany = decimal.Parse(txt_UnemployeCompany.Text);
                _inSurance.BirthCompany = decimal.Parse(txt_BirthCompany.Text);
                _inSurance.InjuryCompany = decimal.Parse(txt_InjuryCompany.Text);
                _inSurance.CompanyTotal = decimal.Parse(RepalceStringFormat(lbl_CompanyTotal));
                _inSurance.SocialTotal = decimal.Parse(RepalceStringFormat(lbl_SocialTotal));
                _inSurance.ProvPerson = decimal.Parse(RepalceStringFormat(lbl_ProvPerson));
                _inSurance.ProvCompany = decimal.Parse(RepalceStringFormat(lbl_ProvCompany));
                _inSurance.ProvTotal = decimal.Parse(RepalceStringFormat(lbl_ProvTotal));
                _inSurance.CompanyAmount = decimal.Parse(RepalceStringFormat(lbl_CompanyAmount));
                _inSurance.PersonAmount = decimal.Parse(RepalceStringFormat(lbl_PersonAmount));
                _inSurance.Amounts = decimal.Parse(RepalceStringFormat(lbl_Amounts));
                _inSurance.Memo = txt_Memo.Text;
                returnvalue = TbsDbSession.Default.Insert<HR_Insurance>(_inSurance);
            }
            if (returnvalue > 0)
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }

        protected string RepalceStringFormat(Label lbl)
        {
            string n = lbl.Text;
            n = n.Replace("<span class='boldblack'>", "").Replace("<span class='boldred'>", "").Replace("</span>", "");
            List<string> ls = StringHelper.GetSubStringList(n, '：');
            return ls[1];
        }

        protected string GetStringFormat(string name, string amount)
        {
            return string.Format("<span class='boldblack'>{0}</span><span class='boldred'>{1}</span>", name, amount);
        }
        protected void Calculation(object sender, EventArgs e)
        {
            CalculationVoid();
        }

        protected void CalculationVoid()
        {
            #region 主合计数据
            double _socialBase = 0;//社保养老基数
            double _provBase = 0;//公积金基数
            double _personTotal = 0;//个人保险总金额
            double _companyTotal = 0;//单位保险合计
            double _socialTotal = 0;//社保总合计
            double _provPerson = 0;//公积金个人缴纳
            double _provCompany = 0;//公积金单位缴纳
            double _provTotal = 0;//公积金合计
            double _personAmount = 0;//个人缴纳总合计
            double _companyAmount = 0;//单位缴纳总合计
            double _amounts = 0;//总合计
            if (txt_SocialBase.Text != "0")
                _socialBase = double.Parse(txt_SocialBase.Text);
            if (txt_ProvBase.Text != "0")
                _provBase = double.Parse(txt_ProvBase.Text);
            
            #endregion

            #region 个人保险缴纳情况
            double _nursPerson = 0;//个人养老保险
            double _medicalPerson = 0;//个人医疗保险
            double _unemployePerson = 0;//个人失业保险
            if (txt_NursPerson.Text != "0")
                _nursPerson = double.Parse(txt_NursPerson.Text);
            if (txt_MedicalPerson.Text != "0")
                _medicalPerson = double.Parse(txt_MedicalPerson.Text);
            if (txt_UnemployePerson.Text != "0")
                _unemployePerson = double.Parse(txt_UnemployePerson.Text);
           // _nursPerson = _socialBase * 0.08; //个人养老保险算法
            _personTotal = _nursPerson + _medicalPerson + _unemployePerson;//个人保险合计
           // txt_NursPerson.Text = _nursPerson.ToString();
            lbl_PersonTotal.Text = string.Format("&nbsp;<span class='boldblack'>合计：</span><span class='boldred'>{0}</span>", _personTotal.ToString());
            #endregion

            #region 单位保险缴纳情况
            double _nursCompany = 0;//单位养老保险
            double _medicalCompany = 0;//单位医疗保险
            double _unemployeCompany = 0;//单位失业保险
            double _birthCompany = 0;//单位生育保险
            double _injuryCompany = 0;//单位工伤保险
           // _nursCompany = _socialBase * 0.21;
            if (txt_NursCompany.Text != "0")
                _nursCompany = double.Parse(txt_NursCompany.Text);
            if (txt_MedicalCompany.Text != "0")
                _medicalCompany = double.Parse(txt_MedicalCompany.Text);

            if (txt_UnemployeCompany.Text != "0")
                _unemployeCompany = double.Parse(txt_UnemployeCompany.Text);

            if (txt_BirthCompany.Text != "0")
                _birthCompany = double.Parse(txt_BirthCompany.Text);

            if (txt_InjuryCompany.Text != "0")
                _injuryCompany = double.Parse(txt_InjuryCompany.Text);

            _companyTotal = _nursCompany + _medicalCompany + _unemployeCompany + _birthCompany + _injuryCompany;

            //txt_NursCompany.Text = _nursCompany.ToString();
            lbl_CompanyTotal.Text = string.Format("&nbsp;<span class='boldblack'>合计：</span><span class='boldred'>{0}</span>", _companyTotal.ToString());
            #endregion

            #region 计算社保总计
            ///社保总计
            _socialTotal = _personTotal + _companyTotal;
            lbl_SocialTotal.Text = GetStringFormat("社保合计：", _socialTotal.ToString());
            #endregion

            #region 计算公积金缴纳
            _provPerson = _provBase * 0.07;
            _provCompany = _provBase * 0.07;
            _provTotal = _provPerson + _provCompany;
            lbl_ProvPerson.Text = GetStringFormat("公积金个人缴纳：", _provPerson.ToString());
            lbl_ProvCompany.Text = GetStringFormat("公积金单位缴纳：", _provCompany.ToString());
            lbl_ProvTotal.Text = GetStringFormat("公积金合计：", _provTotal.ToString());
            #endregion

            #region 总合计
            _personAmount = _personTotal + _provPerson;
            _companyAmount = _companyTotal + _provCompany;
            _amounts = _personAmount + _companyAmount;
            lbl_PersonAmount.Text = GetStringFormat("个人缴纳总合计：", _personAmount.ToString());
            lbl_CompanyAmount.Text = GetStringFormat("单位缴纳总合计：", _companyAmount.ToString());
            lbl_Amounts.Text = GetStringFormat("总合计：", _amounts.ToString());
            #endregion
        }
    }
}